package com.snail.springjpa.demo;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @title: ThreadPoolDemo
 * @Author liwenbo
 * @Date: 2021/8/25 10:38
 */
public class ThreadPoolDemo {

    public static void main(String[] args) {
        AtomicInteger integer = new AtomicInteger(0);
        ArrayBlockingQueue<Runnable> runnables = new ArrayBlockingQueue<>(1);
        ThreadPoolExecutor executor = new ThreadPoolExecutor(2,2,1, TimeUnit.SECONDS,runnables,new ThreadPoolExecutor.CallerRunsPolicy());

        for (int i = 0; i < 10; i++) {
            executor.execute(()->{
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                integer.addAndGet(1);
                System.out.println(String.format("当前时间：%d|当前次数：%d|当前线程：%s", System.currentTimeMillis(), integer.get(), Thread.currentThread().getName()));
            });
        }

        executor.shutdown();
    }
}
